package admin

import (
	"baokaobang/datasourse"
	"baokaobang/models"
	"baokaobang/utils"
	"encoding/json"
	"fmt"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"strings"
)

type CreateMajor struct {
	Id         string `json:"id"`
	CategoryId int64  `json:"categoryId"`
	Name       string `json:"name"`
	Desc       string `json:"desc"`
}

type MajorListParam struct {
	CategoryId int64 `json:"categoryId"`
	CurPage    int   `json:"curPage"`
	PerPage    int   `json:"perPage"`
}

type Major struct {
	Ctx iris.Context
}

func (c *Major) OptionsCreate() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *Major) PostCreate() interface{} {
	createMajor := CreateMajor{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接受请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &createMajor)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("参数解析出错:%s", err.Error()), "")
	}

	if len(createMajor.Name) <= 0 {
		return utils.CommonRes(1000, "请输入专业名称", "")
	}
	if createMajor.CategoryId <= 0 {
		return utils.CommonRes(1000, "请选择专业分类", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	major := models.Major{}
	major.Name = createMajor.Name
	major.CategoryId = createMajor.CategoryId
	major.Desc = createMajor.Desc
	_, err = db.InsertOne(major)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("操作失败：%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "操作成功", "")
}

func (c *Major) OptionsEdit() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *Major) PostEdit() interface{} {
	createMajor := CreateMajor{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接受请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &createMajor)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("参数解析出错：%s", err.Error()), "")
	}

	if len(createMajor.Id) <= 0 {
		return utils.CommonRes(1000, "请提供专业id", "")
	}
	if len(createMajor.Name) <= 0 {
		return utils.CommonRes(1000, "请输入专业名称", "")
	}
	if createMajor.CategoryId <= 0 {
		return utils.CommonRes(1000, "请选择专业分类", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("数据库连接出错：%s", err.Error()), "")
	}

	data := map[string]interface{}{
		"category_id": createMajor.CategoryId,
		"name":        createMajor.Name,
		"desc":        createMajor.Desc,
	}
	_, err = db.Table(new(models.Major)).ID(createMajor.Id).Update(data)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("操作失败:%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "操作成功", "")
}

func (c *Major) OptionsList() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *Major) PostList() interface{} {
	majorListParam := MajorListParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接受请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &majorListParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接受参数出错:%s", err.Error()), "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("数据库连接出错：%s", err.Error()), "")
	}

	start := (majorListParam.CurPage - 1) * majorListParam.PerPage
	majorList := make([]models.MajorObj, 0)
	sess := db.Table(new(Major)).Alias("m").
		Join("INNER", []string{"major_category", "mc"}, "m.category_id = mc.id").
		Select("m.*,mc.*").Limit(majorListParam.PerPage, start)
	if majorListParam.CategoryId > 0 {
		sess = sess.Where("m.category_id = ?", majorListParam.CategoryId)
	}
	err = sess.Find(&majorList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("获取专业列表失败：%s", err.Error()), "")
	}

	countSess := db.Where("id > ?", 0)
	if majorListParam.CategoryId > 0 {
		countSess = countSess.Where("category_id = ?", majorListParam.CategoryId)
	}
	total, err := countSess.Count(new(models.Major))
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("获取专业列表失败:%s", err.Error()), "")
	}

	for i, item := range majorList {
		majorList[i].Major.MajorCreated = item.Major.Created.Format("2006-01-02 15:04")
	}

	data := map[string]interface{}{
		"total": total,
		"list":  majorList,
	}

	return utils.CommonRes(20000, "成功获取专业列表", data)
}

func (c *Major) OptionsDetail() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *Major) PostDetail() interface{} {
	editParam := utils.EditParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &editParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}

	if len(editParam.Id) <= 0 {
		return utils.CommonRes(1000, "参数不完整", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	major := new(models.Major)
	ok, err := db.ID(editParam.Id).Get(major)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("信息获取失败:%s", err.Error()), "")
	}
	if !ok {
		return utils.CommonRes(1000, "信息获取失败", "")
	}

	return utils.CommonRes(20000, "信息获取成功", major)
}

func (c *Major) OptionsDel() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *Major) PostDel() interface{} {
	delParam := utils.DelParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接受请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &delParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("参数解析出错：%s", err.Error()), "")
	}

	if len(delParam.Ids) <= 0 {
		return utils.CommonRes(1000, "请提供要删除项的id", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	ids := strings.Split(delParam.Ids, ",")
	for _, id := range ids {
		major := new(models.Major)
		db.ID(id).Get(major)
		db.ID(id).Delete(major)
	}

	return utils.CommonRes(20000, "删除成功", "")
}
